package vn.edu.saigontech.ncms.web.study;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateOfferingMemberAction extends AbstractValidator {
	
	private Long id;
	private String firstName;
	private String lastName;
	private String gender;
	private String dateOfBirth;
	private String email;
	private String consultedDate;
	private String testDate;
	
	public String check() {
		if(Checker.isEmpty(this.firstName)) {
			this.sendKeyError("validator.required", "firstName");
		}
		if(Checker.isEmpty(this.lastName)) {
			this.sendKeyError("validator.required", "lastName");
		}
		if(Checker.isEmpty(this.gender)) {
			this.sendKeyError("validator.required", "gender");
		}
		if(Checker.isEmpty(this.dateOfBirth)) {
			this.sendKeyError("validator.required", "dateOfBirth");
		}		
		if(!Checker.isDate(this.dateOfBirth, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "dateOfBirth", "format.date");
		}
		if(!Checker.isEmpty(this.email)) {
			if(!Checker.isEmail(this.email)) {
				this.sendKeyError("validator.invalid", "email");
			}
		}
		Student studentSearch = new Student();
		studentSearch.setFirstName(this.firstName);
		studentSearch.setLastName(this.lastName);
		studentSearch.setGender(Gender.create(this.gender));
		studentSearch.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
		Collection<Student> students = StudentService.INSTANCE.find(studentSearch);
		if(students.size() > 1) {
			this.sendKeyError("validator.exist", "student");
		}
		if(!Checker.isEmpty(this.consultedDate)) {
			if(!Checker.isDate(this.consultedDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "consultedDate", "format.date");
			}
		}
		if(!Checker.isEmpty(this.testDate)) {
			if(!Checker.isDate(this.testDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "testDate", "format.date");
			}
		}
		return this.toSuccess();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getConsultedDate() {
		return consultedDate;
	}

	public void setConsultedDate(String consultedDate) {
		this.consultedDate = consultedDate;
	}

	public String getTestDate() {
		return testDate;
	}

	public void setTestDate(String testDate) {
		this.testDate = testDate;
	}
}
